//
//  ZDLoginController.m
//  ZDPension
//
//  Created by 刘甲奇 on 2017/6/14.
//  Copyright © 2017年 maker. All rights reserved.
//

#import "ZDLoginController.h"
#import "NSString+MD5.h"
#import <MJExtension/MJExtension.h>
#import "API.h"
#import "MakerTools.h"
#import "ZDOldManModel.h"

@interface ZDLoginController ()

@property (weak, nonatomic) IBOutlet UITextField *userNameField;

@property (weak, nonatomic) IBOutlet UITextField *passwordField;

@end

@implementation ZDLoginController

- (void)setupUI
{
    [super setupUI];
    self.title = @"登录";
    NSString * userName = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
    if (userName) {
        NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];
        self.userNameField.text = userName;
        self.passwordField.text = password;

    }else{
        self.userNameField.text = @"";
        self.passwordField.text = @"";
    }
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem = leftItem;
}
#pragma mark- 网路请求
- (void)requestLogin
{
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:self.userNameField.text forKey:@"username"];
    [dict setObject:[self.passwordField.text stringToMD5] forKey:@"password"];
    [dict setObject:@"ios" forKey:@"client"];
    [SVProgressHUD showWithStatus:@"正在登录"];
    __weak typeof (self) weakSelf = self;
    [API netAccess:LoginUrl andMethor:@"POST" andDict:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        NSString *code = successCode[@"code"];
        if ([code intValue] == 200) {
            NSDictionary *datas = successCode[@"datas"];
            ZDLog(@"%s---%d---%@",__func__,__LINE__,successCode);
           
            NSString *error = datas[@"error"];
            if (error) {
                [SVProgressHUD showErrorWithStatus:error];
            }else
            {
                ZDOldManModel *oldManModel = [ZDOldManModel mj_objectWithKeyValues:datas];
                //写入文件,保存用户信息
                if ([MakerTools saveToSandBox:OLDMAN_INFO withSavedObject:oldManModel])
                {
                    ZDLog(@"%s---%d---成功写入文件,保存用户信息",__func__,__LINE__);
                }
                 NSString *key = oldManModel.key;
                //保存 key
                [[NSUserDefaults standardUserDefaults] setObject:key forKey:@"key"];
                [[NSUserDefaults standardUserDefaults] setBool:YES forKey:ISLOGIN];
                [[NSUserDefaults standardUserDefaults] synchronize];
                //跳转
                [weakSelf dismissViewControllerAnimated:YES completion:nil];
            }
        }
    } falure:^(NSError *er) {
        [SVProgressHUD dismiss];
        ZDLog(@"%s---%d---%@",__func__,__LINE__,er);
    }];
}
#pragma mark- 点击事件
- (IBAction)loginClick:(UIButton *)sender
{
    if (self.userNameField.text.length == 0)
    {
        [SVProgressHUD showInfoWithStatus:@"用户名不能为空"];
        return;
    }
    if (self.passwordField.text.length < 6)
    {
        [SVProgressHUD showInfoWithStatus:@"密码的长度不能小于6位"];
        return;
    }
//    if (![MakerTools isPhoneNumber:self.userNameField.text])
//    {
//        [SVProgressHUD showInfoWithStatus:@"请输入正确的手机号"];
//        return;
//    }
    //发送登录请求
    [[NSUserDefaults standardUserDefaults] setObject:self.userNameField.text forKey:@"userName"];
    [[NSUserDefaults standardUserDefaults] setObject:self.passwordField.text  forKey:@"password"];
    [self requestLogin];
    
}
- (void)back
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
